#pragma once
#include "common.h"
#include <functional>
#include <unordered_map>
#include <vector>
#include "memory"

class TcpServer {
 public:
  DISALLOW_COPY_AND_MOVE(TcpServer);
  TcpServer();
  ~TcpServer();
  void Start();

  RC NewConnection(int fd);
  RC DeleteConnection(int fd);

  void onConnect(std::function<void(Connection *)> fn);
  void onRecv(std::function<void(Connection *)> fn);

 private:
  std::unique_ptr<EventLoop> m_main_reactor;
  std::unique_ptr<Acceptor> m_acceptor;

  std::unordered_map<int, std::unique_ptr<Connection>> m_connections;
  std::vector<std::unique_ptr<EventLoop>> m_sub_reactors;

  std::unique_ptr<ThreadPool> m_thread_pool;

  /// main_reactor 处理新客户端连接的回调函数
  std::function<void(Connection *)> m_on_connect_cb;
  /// sub_reactor 处理客户端输入的回调函数
  std::function<void(Connection *)> m_on_recv_cb;
};
